package com.otc.controller;

import java.text.ParseException;
import java.util.ArrayList;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.otc.model.Comments;
import com.otc.model.OpinionThatCounts;

@Controller
public class SingleController {

	@RequestMapping("/single1")
	public @ResponseBody
	String hello(@RequestParam(value = "name") String name,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "comment") String comment,
			@RequestParam(value = "idMovie") String movieId,
			@RequestParam(value = "idUser") String userId) {

		String str = "{\"name\": \"" + name + "\",\"email\": \""  
    + email + "\",\"comment\": \"" + comment + "\"}";
		
		Map<String, String> test = new LinkedHashMap<String, String>();
		test.put("test1", name);
		test.put("test2",comment);
		
		java.util.Date dt = new java.util.Date();
		java.text.SimpleDateFormat sdf = 
		     new java.text.SimpleDateFormat("yyyy-MM-dd");

		String currentTime = sdf.format(dt);		
		
		OpinionThatCounts otc = new OpinionThatCounts();
		otc.insertMovieComment(name,Integer.parseInt(movieId),  comment, email,currentTime);
		
		return str;
	}

	@RequestMapping(value = "/single", method = RequestMethod.GET)
	public ModelAndView getSingleBulletin(String id10) throws ParseException  {

		OpinionThatCounts otc = new OpinionThatCounts();

		ModelAndView mav1 = new ModelAndView("Common/single");
		Map<String, Object> modelMap = new LinkedHashMap<String, Object>();
		/*modelMap.put("message", id);
		modelMap.put("movieImage", id2);
		modelMap.put("day", id3);
		modelMap.put("month", id4);
		modelMap.put("dislike", id5);
		modelMap.put("like", id6);
		modelMap.put("rating", id7);
		modelMap.put("username", id8);
		modelMap.put("description", id9);
		modelMap.put("movieId",id10);
		modelMap.put("userId",id11);*/

		modelMap.put("movieId",id10);
		ArrayList<String> allCommentData =otc.retrieveMovieDetailForComments(id10);
		
		String movie_name = allCommentData.get(0);
		String movie_image = allCommentData.get(1);
		String user_id = allCommentData.get(2);
		String movie_description = allCommentData.get(3);
		String movie_like = allCommentData.get(4);
		String movie_dislike = allCommentData.get(5);
		String movie_rating = allCommentData.get(6);
		String movie_time = allCommentData.get(7);		
		String commentMovieDay = movie_time.substring(8, 10);
		String commentMovieMonth = movie_time.substring(5, 7);		
		
		commentMovieMonth = getMonth(Integer.parseInt(commentMovieMonth));

		modelMap.put("moviename",movie_name);
		modelMap.put("movieimage",movie_image);
		modelMap.put("movieuserid",user_id);
		modelMap.put("moviedescription",movie_description);
		modelMap.put("movielike",movie_like);
		modelMap.put("moviedislike",movie_dislike);
		modelMap.put("movierating",movie_rating);
		modelMap.put("movietime",movie_time);		
		
		ArrayList<String> getMovieUserName = otc.retrieveUserNameForComment(user_id);
		String getMovieUserName1 = getMovieUserName.get(0);
		
		ArrayList<Comments> comments = otc.retrieveComments(id10);
		int countComments = otc.retrieveCommentCount(id10);

		modelMap.put("commentCount", countComments);
		modelMap.put("commentData", comments);
		modelMap.put("allCommentData", allCommentData);
		modelMap.put("getMovieUserName", getMovieUserName1);
		modelMap.put("commentMovieDay", commentMovieDay);
		modelMap.put("commentMovieMonth",commentMovieMonth);		
		
		mav1.addAllObjects(modelMap);
		return mav1;
	}
	
	public String getMonth(int month) 
	{
		String[] mon = {"JAN","FEB","MAR","APR","MAY","JUN","JUL",
						"AUG","SEP","AUG","NOV","DEC"};
			return mon[month-1];
	}	
}